<template>
  <div>
    <div>
      <el-tree
        ref="tree"
        check-strictly
        :data="list"
        show-checkbox
        default-expand-all
        node-key="id"
        :props="{label:'name'}"
      />
    </div>
    <!-- @click="setCheckedKeys" -->

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmit">确 定</el-button>
      <el-button @click="$emit('update:showDialogAssign', false)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData2 } from '@/utils/index'
import { assignPerm, getRoleDetail } from '@/api/settings'

export default {
  name: 'VueHrAssignPermission',
  props: {
    showDialogAssign: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
      // permIds: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadRoleDetail()
  },
  methods: {
    // 获取权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData2(res.data)
      console.log('list', this.list)
    },

    // 获取权限详情
    async loadRoleDetail() {
      const res = await getRoleDetail(this.id)
      console.log('getRoleDetail', res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },

    // 点击提交按钮
    async doSubmit() {
      try {
        await assignPerm({ id: this.id, permIds: this.$refs.tree.getCheckedKeys() })
        this.$message.success('分配成功')
        this.$emit('update:showDialogAssign', false)
      } catch (error) {
        console.log(error)
        this.$message.error('分配失败')
      }
    }
  }
}
</script>

<style scoped>

</style>
